<template>
    <span>
        <Button type="success" size="small" @click="handleArchiveLogsShow">归档</Button>
        <Modal v-model="archive_logs_modal" title="日志归档" @on-ok="handleArchiveLogsOk" @on-cancel="handleArchiveLogsCancel" :mask-closable="false">
            <Form :label-width="100">
                <FormItem label="归档日期至">
                    <p>
                        <Button @click="handleChangeDate(0, 0, 1)" size="small" style="margin-right:5px;">昨天</Button>
                        <Button @click="handleChangeDate(0, 1, 0)" size="small" style="margin-right:5px;">一个月前</Button>
                        <Button @click="handleChangeDate(0, 2, 0)" size="small" style="margin-right:5px;">两个月前</Button>
                        <Button @click="handleChangeDate(0, 3, 0)" size="small" style="margin-right:5px;">三个月前</Button>
                        <Button @click="handleChangeDate(0, 6, 0)" size="small" style="margin-right:5px;">半年前</Button>
                        <Button @click="handleChangeDate(1, 0, 0)" size="small" style="margin-right:5px;">一年前</Button>
                    </p>
                    <DatePicker type="date" v-model="archive_date" placeholder="选择日期" style="width: 200px"></DatePicker>
                    <p style="color:#aaa;">请选择今天之前的日期，今天的日期不会被归档</p>
                </FormItem>
            </Form>
        </Modal>
    </span>
</template>

<script>
export default {
    name: 'ClientLogArchiveButton',
    data () {
        return {
            archive_logs_modal: false,
            archive_date: new Date(),
        }
    },
    methods: {
        handleChangeDate(year, month, day) {
            var date = new Date();
            date.setFullYear(date.getFullYear() - year);
            date.setMonth(date.getMonth() - month);
            date.setDate(date.getDate() - day);
            this.archive_date = date;
        },
        handleArchiveLogsShow() {
            // 默认为一个月前的时间
            var date = new Date();
            date.setMonth(date.getMonth() - 1);
            this.archive_date = date;
            this.archive_logs_modal = true;
        },
        handleArchiveLogsOk() {
            let format_date = this.$Date(this.archive_date).format('YYYY-MM-DD');
            this.$http.post('/archive_logs', { limit_time: format_date }).then((res) => {
                if (res.status == 'ok') {
                    this.$Message.success('归档成功');
                    this.$emit('success');
                } else {
                    this.errorMessage(res.data);
                }
                this.archive_logs_modal = false;
            });
        },
        handleArchiveLogsCancel() {
            this.archive_logs_modal = false;
        },
    }
}
</script>